package com.zhike.service.serviceimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.zhike.mapper.MessageMapper;
import com.zhike.mapper.UserMapper;
import com.zhike.model.NewMessage;
import com.zhike.model.Users;
import com.zhike.model.WorkFlowTask;
import com.zhike.service.MessageService;
import com.zhike.util.BasicsUtil;
import com.zhike.util.DataSourceEnum;
import com.zhike.util.DataSourceHolder;
import com.zhike.util.DateUtil;
import com.zhike.util.Jdpush;
import com.zhike.util.JsonUtil;

import cn.jpush.api.report.UsersResult.User;


@Service
public class MessageServiceImpl implements MessageService {
	
	@Resource
	MessageMapper messageMapper;

	@Resource
	UserMapper userMapper;
	@Override
	public String messagePush(NewMessage message) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		//根据id查询具体消息细节
		NewMessage detail  =  messageMapper.messageDetail(message);
		
		Users users = new Users();
		users.setId(message.getReceiveId());
		Users detailUser = userMapper.getDetailUser(users);
			
		String account_ID = detailUser.getAccount_ID();
		Map<String, String> parm1 = new HashMap<>();
		
		Map<String, String> parm = new HashMap<>();
		parm.put("title", detail.getType());
		parm1.put("title", detail.getType());
		String dateTime = detail.getDateTime();
		String newDate = dateTime.substring(0, 19);
		parm.put("msg", detail.getContext()+"         "+newDate);
		parm1.put("msg", detail.getContext()+"         "+newDate);
		parm.put("alias",detail.getRecipientId().replaceAll("-", ""));
		parm1.put("alias",account_ID.replaceAll("-", ""));
		parm.put("messageType", "待办事项");
		parm1.put("messageType", "待办事项");
		Jdpush.jpushAlias(parm);
		Jdpush.jpushAlias(parm1);
		return JsonUtil.blank(true, 200, "调用成功");
	}

	@Override
	public String messagePushLaboratory(NewMessage message) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
			//json字符串转换为对象
			Gson gson = new Gson();
			String data = message.getData();
			NewMessage newMessage = gson.fromJson(data, NewMessage.class);	
			//获取对象属性推送
		
		
			for (int i = 0; i < newMessage.getFlowPushReceiveInfoList().length; i++) {
				//获取两个别名
				String receiveId = newMessage.getFlowPushReceiveInfoList()[i].getReceiveId();
				Users users = new Users();
				users.setId(receiveId);
				Users detailUser = userMapper.getDetailUser(users);
				String account_ID = detailUser.getAccount_ID();
				String id = detailUser.getId();
				
				Map<String, String> parm = new HashMap<>();
				parm.put("title",newMessage.getTitle());
				String timeStamp3Date = DateUtil.timeStamp3Date(newMessage.getSendTimeTicks(),"yyyy-MM-dd HH:mm:ss");
				parm.put("msg", newMessage.getContent()+"         "+timeStamp3Date);
				parm.put("messageType", "待办事项");
				parm.put("alias",account_ID.toUpperCase().replaceAll("-", ""));
				Jdpush.jpushAlias(parm);
				
				Map<String, String> parm1 = new HashMap<>();
				parm1.put("title",newMessage.getTitle());  
				parm1.put("msg", newMessage.getContent()+"         "+timeStamp3Date);
				parm1.put("messageType", "待办事项");
				parm1.put("alias",id.toUpperCase().replaceAll("-", ""));
				Jdpush.jpushAlias(parm1);
			}
			
			return JsonUtil.blank(true, 200, "调用成功");
		
	}

	@Override
	public String messageListLaboratory(WorkFlowTask workFlowTask) {
		//连接实验室管理系统数据库
		DataSourceHolder.setDataSources(DataSourceEnum.DS2.getKey());
		WorkFlowTask workFlowTask2 = messageMapper.messageListLaboratory(workFlowTask);
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add(workFlowTask2);
		return jsonUtil.getJson();
	}
	
	
	@Override
	public String backlogList(NewMessage message) {
		//通过id查看这个用户绑定的资料
		Users users = new Users();
		users.setId(message.getRecipientId());
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		
		Users users2 = userMapper.getDetailUser(users);
		users.setId(message.getSenderID());
		
	//通过id查询 待办事项的具体资料
		//绩效管理系统的待办事项 存进list
		JsonUtil ju = new JsonUtil();
			 message.setRecipientId(users2.getId().toLowerCase());
				List<NewMessage> list = new  ArrayList<NewMessage>();
					list = messageMapper.messageDetailList(message);				
					

				//实验室管理系统的待办事项  存进list 
			List<WorkFlowTask> wList = new  ArrayList<WorkFlowTask>();
		
			
			if (message.getMesState().equals(0)) {
				//连接实验室管理系统数据库
				DataSourceHolder.setDataSources(DataSourceEnum.DS2.getKey());
				WorkFlowTask workFlowTask = new WorkFlowTask();
				//id转换为大写数据库操作
				workFlowTask.setReceiveID(users2.getAccount_ID().toUpperCase());
				wList = messageMapper.workFlowTaskListById(workFlowTask);
				for (int i = 0; i < wList.size(); i++) {
					WorkFlowTask workFlowTask2 = wList.get(i);
					
					//查出flowid
					String flowID = workFlowTask2.getFlowID();
					//根据flowid 查询出json  解析json 拿到表名
					String json = messageMapper.getRunJson(flowID);
					String jxjsonbm = BasicsUtil.JXJSONBM(json);
					//表名和实例 返回
					String serderName = userMapper.getUserDetil(workFlowTask2.getSenderID());
					NewMessage newMessage = new NewMessage();
					newMessage.setType(workFlowTask2.getTitle());
					newMessage.setName(workFlowTask2.getReceiveName());
					newMessage.setPrevStepID(workFlowTask2.getPrevStepID());
					newMessage.setSlID(workFlowTask2.getInstanceID());
					newMessage.setSjkbm(jxjsonbm);
					newMessage.setLimsId(workFlowTask2.getID());
					newMessage.setRecipientId(workFlowTask2.getReceiveID());
					newMessage.setUserId(workFlowTask2.getReceiveID());
					newMessage.setStepName(workFlowTask2.getStepName());
					newMessage.setStepID(workFlowTask2.getStepID());
					newMessage.setMesState(workFlowTask2.getStatus());
					newMessage.setDateTime(workFlowTask2.getReceiveTime());
					newMessage.setUserName(workFlowTask2.getReceiveName());
					newMessage.setFlowID(workFlowTask2.getFlowID());
					newMessage.setGroupID(workFlowTask2.getGroupID());
					newMessage.setInstanceID(workFlowTask2.getInstanceID());
					newMessage.setSenderID(workFlowTask2.getSenderID());
					newMessage.setParentId(workFlowTask2.getPrevID());
					newMessage.setSenderName(serderName);
					list.add(newMessage);
				}
				
				
				
			}
			
			
			//已完成的消息列表
			if (message.getMesState().equals(1)) {
				//连接实验室管理系统数据库
				DataSourceHolder.setDataSources(DataSourceEnum.DS2.getKey());
				WorkFlowTask workFlowTask = new WorkFlowTask();
				//id转换为大写数据库操作
				workFlowTask.setReceiveID(users2.getAccount_ID().toUpperCase());
				wList = messageMapper.workFlowTaskListById1(workFlowTask);
				
				//循环添加进list
				for (int i = 0; i < wList.size(); i++) {
					WorkFlowTask workFlowTask2 = wList.get(i);
					//查出flowid
					String flowID = workFlowTask2.getFlowID();
					//根据flowid 查询出json  解析json 拿到表名
					String json = messageMapper.getRunJson(flowID);
					String jxjsonbm = BasicsUtil.JXJSONBM(json);
					String serderName = userMapper.getUserDetil(workFlowTask2.getSenderID());
					//表名和实例 返回
					NewMessage newMessage = new NewMessage();
					newMessage.setType(workFlowTask2.getTitle());
					newMessage.setPrevStepID(workFlowTask2.getPrevStepID());
					newMessage.setLimsId(workFlowTask2.getID());
					newMessage.setRecipientId(workFlowTask2.getReceiveID());
					newMessage.setUserId(workFlowTask2.getReceiveID());
					newMessage.setSlID(workFlowTask2.getInstanceID());
					newMessage.setSjkbm(jxjsonbm);
					newMessage.setName(workFlowTask2.getReceiveName());
					newMessage.setStepName(workFlowTask2.getStepName());
					newMessage.setParentId(workFlowTask2.getPrevID());
					newMessage.setMesState(workFlowTask2.getStatus());
					newMessage.setDateTime(workFlowTask2.getReceiveTime());
					newMessage.setUserName(workFlowTask2.getReceiveName());
					newMessage.setStepID(workFlowTask2.getStepID());
					newMessage.setFlowID(workFlowTask2.getFlowID());
					newMessage.setGroupID(workFlowTask2.getGroupID());
					newMessage.setInstanceID(workFlowTask2.getInstanceID());
					newMessage.setSenderID(workFlowTask2.getSenderID());
					newMessage.setSenderName(serderName);
					list.add(newMessage);
				}
			} 
			
			//list时间戳
			for (int i = 0; i < list.size(); i++) {
				NewMessage newMessage = list.get(i);
				newMessage.setDateTime(newMessage.getDateTime().substring(0, 19));
			}
			
			DateUtil.ListSortN(list);
			
			//存进list之后进行分页
		    List<NewMessage> NewMessageBeanPage = new ArrayList<NewMessage>();
	        int currIdx = (message.getPage() > 1 ? (message.getPage() -1) * message.getPageSize() : 0);
	        for (int i = 0; i < message.getPageSize() && i < list.size() - currIdx; i++) {
	        	NewMessage nMessage = list.get(currIdx + i);
	        	NewMessageBeanPage.add(nMessage);
	        }
			ju.setList(NewMessageBeanPage);
		return ju.getJson();
		
	}

	@Override
	public String backlogCount(NewMessage message) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		//通过id查看这个用户绑定的资料
				Users users = new Users();
				users.setId(message.getRecipientId());
				Users users2 = userMapper.getDetailUser(users);
				//实验室管理系统id
				String account_ID = users2.getAccount_ID();
				//绩效管理系统id
				String id = users2.getId();
				
				
		//如果是绩效管理系统登录
		JsonUtil js = new JsonUtil();
			message.setRecipientId(users2.getId().toLowerCase());
			Integer integer= messageMapper.messageCount(message);
	
			//连接实验室管理系统数据库
			DataSourceHolder.setDataSources(DataSourceEnum.DS2.getKey());
			 message.setRecipientId(users2.getAccount_ID().toUpperCase());
			 Integer i=0;
			 if (message.getMesState().equals(0)) {
				i= messageMapper.workFlowTaskCount(message);
			}
			 if (message.getMesState().equals(1)) {
					i= messageMapper.workFlowTaskCount1(message);
				}
			Integer count =  i+integer;
			message.setCount(count);
			js.add(message);
			return js.getJson();
	}

	
}
